<?php
/**
 * Create a new Customer
 */
use Fox\Generator;
use Shama\Contact;
use Fox\Services;
use Shama\Messages;
use Fox\SalesDistribution\Sales\Partner\Role;
use Fox\Common\Customer\Partner;
use Fox\Html\Header\Image;
use Fox\Html\Wrapper,Fox\Html\TabPanel,Fox\Html\TabPanel\Tab,Fox\Html\Screen;
use Fox\Common\Customer;

$screen = new Screen("sContact","create");
$screen->mainTitle(new Image("contact",Msg(Messages::BUNDLE,Messages::TITLE_CONTACT_CREATE)));
$blHeader = Xview::instance()->component("contacts", "header");
$blHeader->item("owner")->initialize(User::current()->getId());
$screen->add($blHeader);
$blName = Xview::instance()->component("contacts", "name");
$screen->add($blName);
$blAddress = Xview::instance()->component("contacts", "address");
$screen->add($blAddress);
$blAdditonal = Xview::instance()->component("contacts", "additional");
$screen->add($blAdditonal);
$blComment = Xview::instance()->component("contacts", "comment");
$screen->add($blComment);
$screen->optimizeWidth();

if($screen->submitted())
{
	$inputs = $screen->get();
	if(!$screen->errors())
	{
		try {
			$response = Services::instance()->service(Contact::SERVICE_CREATE)->call($inputs);
			$response = Fox\Utilities::jsonUTF8Decode($response);
			if(isset($response['errors']))
			{
				throw new Exception($response['errors'][0]);
			}
			else
			{
				Fox\Message::Success(Msg(Messages::BUNDLE,Messages::MSG_CONTACT_CREATED,$response['id']));
				$screen->redirect(Generator::url('contacts','update',null,array('inLineId' => $response['id'])));
			}
		}
		catch(Exception $e)
		{
			Fox\Message::Error($e->getMessage());
		}
	}
}
echo $screen->Display();